﻿using System;
using System.Data;

namespace GeneratorLibrary.Client.Base {
    public abstract class CrudColumnBase {
        private CrudColumnBase() { }
        public CrudColumnBase(DataRow dr){
            name = dr["ColumnName"].ToString();
            isIdentity = ((int)dr["IsIdentity"] == 1);
            isPrimaryKey = ((int)dr["IsPrimaryKey"] == 1);
            isForeignKey = ((int)dr["IsForeignKey"] == 1);
            dataType = dr["DataType"].ToString();
            isNullable = ((int)dr["IsNullable"] == 1);
            isUniqueConstraint = ((int)dr["IsUniqueConstraint"] == 1);
            
            //integer values
            length = dr.IsNull("Length") ? 0 : Convert.ToInt32(dr["length"]);
            columnOrder = Convert.ToInt32(dr["columnOrder"]);
            numericScale = dr.IsNull("NumericScale") ? 0 : Convert.ToInt32(dr["NumericScale"]);
            numericPrecision = dr.IsNull("NumericPrecision") ? 0 : Convert.ToInt32(dr["NumericPrecision"]);
            charMaxLength = dr.IsNull("CharMaxLength") ? 0 : Convert.ToInt32(dr["CharMaxLength"]);

            qualifiedDataType = getQualifiedDataType();
        }


        private int length, columnOrder, numericScale, numericPrecision, charMaxLength;

        string qualifiedDataType,name,dataType;

        bool isIdentity,isPrimaryKey,isNullable,isForeignKey,isUniqueConstraint;
        
        public int CharMaxLength {
            get { return charMaxLength; }
        }
        public int NumericPrecision {
            get { return numericPrecision; }
        }
        public int NumericScale {
            get { return numericScale; }
        }
        public int ColumnOrder {
            get { return columnOrder; }
        }
        public int Length {
            get { return length; }
        }

        public string QualifiedDataType {
            get { return qualifiedDataType; }
        }
        public string Name {
            get { return name; }
        }
        public bool IsIdentity {
            get { return isIdentity; }
        }
        public bool IsPrimaryKey {
            get { return isPrimaryKey; }
        }

        public string DataType {
            get { return dataType; }
        }


        public bool IsNullable {
            get { return isNullable; }
        }

        public bool IsForeignKey {
            get { return isForeignKey; }
        }


        public bool IsUniqueConstraint {
            get { return isUniqueConstraint; }
        }
        public abstract string GetDefaultCoalesce();
        protected abstract string getQualifiedDataType();
    }
}
